package market

import (
	"context"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/constants"
	pb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/market"
	"git.myscrm.cn/golang/stark/v4"
)

type outboundAccountRepository struct {
}

func NewOutboundAccountRepository() OutboundAccountRepositoryIface {
	return &outboundAccountRepository{}
}

func (o *outboundAccountRepository) GetPhoneList(ctx context.Context, request *pb.GetPhoneListRequest) (*pb.GetPhoneListResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetPhoneList request:%+v,GetClientConn err: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundAccountServiceClient(conn)
	response, err := client.GetPhoneList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetPhoneList request:%+v, err: %+v", request, err)
		return nil, err
	}
	return response, nil
}

func (o *outboundAccountRepository) GetSeatInfoList(ctx context.Context, request *pb.GetSeatInfoListRequest) (*pb.GetSeatInfoListResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetSeatInfoList request:%+v,GetClientConn err: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundAccountServiceClient(conn)
	response, err := client.GetSeatInfoList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetSeatInfoList request:%+v, err: %+v", request, err)
		return nil, err
	}
	return response, nil
}

func (o *outboundAccountRepository) GetRobotList(ctx context.Context, request *pb.GetRobotListRequest) (*pb.GetRobotListResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetRobotList request:%+v,GetClientConn err: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundAccountServiceClient(conn)
	response, err := client.GetRobotList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetRobotList request:%+v, err: %+v", request, err)
		return nil, err
	}
	return response, nil
}

func (o *outboundAccountRepository) GetAccountDetail(ctx context.Context, request *pb.GetAccountDetailRequest) (*pb.GetAccountDetailResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetAccountDetail request:%+v,GetClientConn err: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundAccountServiceClient(conn)
	response, err := client.GetAccountDetail(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetAccountDetail request:%+v, err: %+v", request, err)
		return nil, err
	}
	return response, nil
}

func (o *outboundAccountRepository) SaveAccount(ctx context.Context, request *pb.SaveAccountRequest) (*pb.SaveAccountResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "SaveAccount request:%+v,GetClientConn err: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundAccountServiceClient(conn)
	response, err := client.SaveAccount(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "SaveAccount request:%+v, err: %+v", request, err)
		return nil, err
	}
	return response, nil
}


func (o *outboundAccountRepository) ExpenseDetail(ctx context.Context, request *pb.ExpenseDetailRequest) (*pb.ExpenseDetailResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_OPPTY_SERVICE)
	if err != nil {
		stark.Logger.Errorf(ctx, "ExpenseDetail request:%+v,GetClientConn err: %+v", request, err)
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()

	client := pb.NewOutboundAccountServiceClient(conn)
	response, err := client.ExpenseDetail(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "ExpenseDetail request:%+v, err: %+v", request, err)
		return nil, err
	}
	return response, nil
}
